<?php

if(isset($_COOKIE["is_visited"]) && $_COOKIE["is_visited"] == 1){
	exit;
}

require_once '../../source/core/run.php';
$taskConfigFile = CHENCY_ROOT.'data/cache/task_config.php';
setcookie('is_visited', 1, time()+3600);
if(file_exists($taskConfigFile)){
	$taskConfig= require $taskConfigFile;
	if(!empty($taskConfig) && $taskConfig['flag'] == 1){
		if($_SERVER['REQUEST_TIME'] > $taskConfig['start_time'] && (!$taskConfig['end_time'] || $_SERVER['REQUEST_TIME'] < $taskConfig['end_time'])){
			if($_SERVER['REQUEST_TIME'] - $taskConfig['lastUpdate'] > $taskConfig['interval_time']){
				$sql = 'SELECT `id` FROM '.DB_PREFIX.'info WHERE `flag` = 0 ORDER BY `timeline` LIMIT 2';
				$result = $db->getall($sql);
				if(!empty($result)){
					$id_arr = '';
					foreach($result as $key=>$val){
						$id_arr[] = $val['id'];
					}
					$idStr = implode(',', $id_arr);
					if($db->update(DB_PREFIX.'info', array('flag' => 1,'timeline' => $_SERVER['REQUEST_TIME']),"id in ($idStr)")){
						$taskConfig['lastUpdate'] = $_SERVER['REQUEST_TIME'];
						if(file_exists($taskConfigFile)){
							$configStr = "<?php \nreturn ".var_export($taskConfig,true).';';
							file_put_contents($taskConfigFile, $configStr);
						}
					}
				}
			}
		}
	}
}